/*
 * DMatrix.java -
 * Copyright (c) 2005 Carmine Lia
 * e-mail:      carmine.lia@libero.it
 * web-address: http://www.intermedia.sa.it/lia

 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */
package javax.robotics.vecmath;

import javax.vecmath.MismatchedSizeException;

/**
 * Diagonal Matrix, a double precision, general, dynamically-resizable, one-dimensional vector
 * class. Index numbering begins with zero. 
 * -
 * @author <a href="http://www.intermedia.sa.it/lia/">Carmine Lia</a>
 * @since 1.0.2
 * @version 5/10/2005
 */
public class DMatrix extends RVector
{
    /**
     * 
     */
    private static final long serialVersionUID = 3258412850207537208L;
    
    /**
     * Constructs a zero diagonal matrix size by size 
     * @param size the dimension of matrix
     */
    public DMatrix(int size)
    {
        super(size);
    }
    
    /**
     * Constructs a new diagonal matrix from an array.
     * @param diag the input array.
     */
    public DMatrix(double... diag)
    {
        super(diag);
    }
    
    /**
     * Constructs a new diagonal matrix from a vector.
     * @param diag the input vector.
     */
    public DMatrix(RVector diag)
    {
        super(diag);
    }
    
    /**
     * Constructs a new diagonal matrix from another diagonal matrix.
     * @param diag the input diagonal matrix.
     */
    public DMatrix(DMatrix diag)
    {
        super(diag);
    }
    
    /**
     * Constructs a new diagonal matrix from a diagonal of input matrix.
     * @param diag the input matrix.
     */
    public DMatrix(Matrix diag)
    {
        super(diag.getDiagonal());
    }
    
    /**
     * Gets this diagonal matrix as generic matrix.
     * @param M the matrix to be copied
     */
    public void get(Matrix M)
    {
        M.setZero();
        M.setDiagonal(this);
    }
    
    /**
     * Sets the value of return matrix to the result of multiplying this matrix with matrix M (this * M). 
     * @param M the generic input matrix
     * @return the product matrix
     */
    public Matrix mul(Matrix M)
    {
      final int size = this.getSize();
      final int NRow = M.getNumRow();
      final int NCol = M.getNumCol();
      final  Matrix P = new Matrix(NRow, NCol); 
      if( size != NRow)
          throw new MismatchedSizeException("The matrices dimension are not agree.");
      
     for(int i=0; i < size; i++)
         for(int j=0; j < NCol; j++)
             P.setElement(i, j, this.getElement(i)*M.getElement(i,j));
     return P;
    }
    
    /**
     * Invert diagonal matrix in place.
     */
    public void invert()
    {
        final int n = this.getSize();
        for(int i=0; i<n; i++)
            this.setElement(i, 1.0/this.getElement(i));
            
    }
}
